# -*- coding: UTF-8 -*-

##########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

#扫描你的脸，得到一个小视频
# Scan your face to get a small video

from builtins import print
import requests
import imp
import utils.common.httpclient as httpclient
import utils.common.log as log
import utils.common.func as commfunc
import json
import uuid
import base64
import datetime
import os
import threading
import queue
import time
import utils.common.initres as initres



if initres.COS_TYPE :
    import utils.oss.cos as oss   
else :
    import utils.oss.oss as oss   


def vlog_create(imgData, userId, caseId, userName,location,channelId):
    
    templateList=[]
    retBool = False  


    url = f"http://127.0.0.1:33033/api/v1/party/app/mission/case/{caseId}"

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }

    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                templateList = retdic["data"]
                retBool = True
            else:
                log.logger.error("post app mission failed {}".format(res.text))

    
    log.logging.info("templateList len: {}".format(len(templateList)))

    if retBool == False: 
        return ({"code": 201, "error": "未成功获取到活动", "jobId": ""})

 
    camerid_list = [] 

    url = f"http://127.0.0.1:33033/api/v1/party/app/case/{caseId}/device"

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }
    ret = {}
    retBool = False
    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                device_info = retdic["data"]
                retBool = True
            else:
                log.logger.error("get_web_getCaseDevice failed {}".format(res.text))

    for item in device_info :
        camerid_list.append(item["deviceGbCode"])
    
    log.logging.info("camerid list : {}".format(json.dumps(camerid_list)))

    if retBool == False: 
       return ({"code": 201, "error": "未成功获取到设备列表", "jobId": ""})

    jobInfo = {
        "imgData":imgData,
        "userId":userId,
        "caseId":caseId,
        "userName":userName,
        "location":location,
        "channelId":channelId

    }

    log.logging.info("userId*------- :  {}".format(userId))
    ret,jobId = httpclient.post_vlogclip_algorithm_compose(jobInfo, templateList, camerid_list) 
    if ret is False:
        return ({"code": 201, "error": "系统繁忙，请重试", "jobId": ""})
    else:
        log.logging.info("post_vlogclip_algorithm_compose success !")

    return ({"code": 200, "error": "", "jobId": jobId})